$(function () {
    let layer = layui.layer
    let form = layui.form
    let laypage = layui.laypage
    // 定义一个查询的参数对象，将来请求数据的时候，
    // 需要将请求参数对象提交到服务器
    let q = {
        pagenum: 1, // 页码值，默认请求第一页的数据
        pagesize: 5, // 每页显示几条数据，默认每页显示5条
        cate_id: '', // 文章分类的 Id
        state: '' // 文章的发布状态
    }
    initTable()
    initCate()

    function initTable() {
        $.ajax({
            method: 'GET',
            url: '/my/article/list',
            data: q,
            success: res => {
                const {
                    status
                } = res
                if (status !== 0) {
                    return layer.msg('获取文章列表失败!')
                }
                let htmlStr = template('tpl-table', res)
                $('tbody').html(htmlStr)
                renderPage(res.total)
            }
        })
    }

    template.defaults.imports.dataFormat = function (date) {
        let fd = new Date(date)

        let y = fd.getFullYear()
        let m = (fd.getMonth() + 1).toString().padStart(2, '0')
        let d = fd.getDate().toString().padStart(2, '0')
        let hh = fd.getHours().toString().padStart(2, '0')
        let mm = fd.getMinutes().toString().padStart(2, '0')
        let ss = fd.getSeconds().toString().padStart(2, '0')

        return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
    }

    function initCate() {
        $.ajax({
            method: 'GET',
            url: '/my/article/cates',
            success: res => {
                const {
                    status
                } = res
                if (status !== 0) {
                    return layer.msg('获取分类数据失败!')
                }

                let htmlStr = template('tpl-cate', res)
                $('[name=cate_id]').html(htmlStr)
                form.render()
            }
        })
    }

    $('#form-search').on('submit', function (e) {
        e.preventDefault()
        let cate_id = $('[name=cate_id]').val()
        let state = $('[name=state]').val()

        q.cate_id = cate_id
        q.state = state

        initTable()
    })

    function renderPage(total) {
        laypage.render({
            elem: 'test1', //注意，这里的 test1 是 ID，不用加 # 号
            count: total, //数据总数，从服务端得到
            limit: q.pagesize,
            curr: q.pagenum,
            layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
            limits: [1, 2, 3, 4, 5, 10, 15, 20],
            jump: (obj, first) => {
                q.pagenum = obj.curr
                q.pagesize = obj.limit
                if (!first) {
                    initTable()
                }
            }
        });
    }

    $('tbody').on('click', '.btn-delete', function () {
        let len = ('.btn.delete').length
        let id = $(this).attr('data-id')
        layer.confirm('确认删除?', {
            icon: 3,
            title: '提示'
        }, function (index) {
            $.ajax({
                method: 'GET',
                url: '/my/article/delete/' + id,
                success: res => {
                    const {
                        status
                    } = res
                    if (status !== 0) {
                        return layer.msg('删除文章失败!')
                    }

                    layer.msg('删除文章成功!')

                    if (len === 1 && q.pagenum !== 1) {
                        q.pagenum = q.pagenum - 1
                    }
                    initTable()
                    layer.close(index);
                }
            })
        })
    })
})